
#define TestSet_Variable

#include "userDef.h"

void set_captest(U8 value)
{
    if (CapTestStat == value)
    {
        ;
    }
    else
    {
        SET(f_upd_set_cap_test);

        CapTestStat = value;

        if (CapTestStat != 0)
        {
            if (e_mode.set == MODEHEAT)
            {
                set_tartemp(310, 880);
            }
            else
            {
                set_tartemp(160, 610);
            }

            BuzzerAct(BUZZER400ms);
        }

        set_comp_freq(0);
    }
}

void set_exp_step(U16 value)
{
    if (SetExpValue == value)
    {
        ;
    }
    else
    {
        SET(f_upd_set_exp_step);

        SetExpValue = value;

        BuzzerAct(BUZZER400ms);
    }
}

void set_exh_temp(U8 value)
{
    if (SetExhValue == value)
    {
        ;
    }
    else
    {
        SET(f_upd_set_exh_temp);

        SetExhValue = value;

        if (SetExhValue > 250)
        {
            SetExhValue = 250;
        }

        BuzzerAct(BUZZER400ms);
    }
}

void set_comp_freq(U8 value)
{
    if (SetFreValue == value)
    {
        ;
    }
    else
    {
        SET(f_upd_set_comp_freq);

        SetFreValue = value;

        if (SetFreValue > 250)
        {
            SetFreValue = 250;
        }

        if (e_mode.set == MODEHEAT)
        {
            set_tartemp(310, 880);
        }
        else
        {
            set_tartemp(160, 610);
        }

        BuzzerAct(BUZZER400ms);
    }
}

void set_out_fan(U16 value)
{
    if (SetOutFanValue == value)
    {
        ;
    }
    else
    {
        SET(f_upd_set_out_fan);

        SetOutFanValue = value;

        if (SetOutFanValue >= 2500)
        {
            SetOutFanValue = 2500;
        }

        BuzzerAct(BUZZER400ms);
    }
}

void set_in_fan(U16 value)
{
    if (SetInFanValue == value)
    {
        ;
    }
    else
    {
        SET(f_upd_set_in_fan);

        SetInFanValue = value;

        if (SetInFanValue >= 2500)
        {
            SetInFanValue = 2500;
        }

        BuzzerAct(BUZZER400ms);
    }
}

void set_sw_step(U8 value)
{
    if (SetSwStepValue == value)
    {
        ;
    }
    else
    {
        SET(f_upd_set_sw_step);

        SetSwStepValue = value;

        if (SetSwStepValue > 0xFE)
        {
            SetSwStepValue = 0xFE;
        }

        BuzzerAct(BUZZER400ms);
    }
}

void set_correct(U8 value)
{
    if (setCorreck == value)
    {
        ;
    }
    else
    {
        SET(f_upd_set_correct);

        setCorreck = value;

        switch (setCorreck)
        {
        case 0:
            break;

        case 1:
            temp.repair = 0;
            f_code_00 = 1;
            break;

        case 2:
            temp.repair = 10;
            f_code_01 = 1;
            break;

        case 3:
            temp.repair = 20;
            f_code_02 = 1;
            break;

        case 4:
            temp.repair = 30;
            f_code_03 = 1;
            break;

        default:
            break;
        }

        f_eepRomW = 1;
        BuzzerAct(BUZZER400ms);
    }
}

void set_app_set_test(U8 value)
{
    if (setAppSetTest == value)
    {
        ;
    }
    else
    {
        SET(f_upd_app_set_test);

        setAppSetTest = value;

        if (setAppSetTest)
        {
            f_code_oA = 1;
        }
    }
}

void set_app_show_data(U8 value)
{
    if (setAPPShowData == value)
    {
        ;
    }
    else
    {
        SET(f_upd_app_show_data);

        setAPPShowData = value;

        if (setAPPShowData)
        {
            f_code_oC = 1;
        }
    }
}

void TestStatSet(void)
{
    LightJudge();
    SleepJudge();
    CaptestSet();
}

void LightJudge(void)
{
    U16 TempA;

    if (b_power.set == ON && f_chg_light == TRUE)
    {
        if ((e_mode.set == MODECOOL && l_tarTemp_C.set[e_mode.set] == 160) || (e_mode.set == MODEHEAT && l_tarTemp_C.set[e_mode.set] == 300))
        {
            if (SetFreTime == 0)
            {
                SetFreTime = 30;
                SetFreCount = 1;
            }
            else
            {
                SetFreCount++;
            }
        }
        else if ((e_mode.set == MODECOOL && l_tarTemp_C.set[e_mode.set] == 170) || (e_mode.set == MODEHEAT && l_tarTemp_C.set[e_mode.set] == 290))
        {
            if (SetExpTime == 0)
            {
                SetExpTime = 30;
                SetExpCount = 1;
            }
            else
            {
                SetExpCount++;
            }
        }
        else if ((e_mode.set == MODECOOL && l_tarTemp_C.set[e_mode.set] == 180) || (e_mode.set == MODEHEAT && l_tarTemp_C.set[e_mode.set] == 280))
        {
            if (SetExhTime == 0)
            {
                SetExhTime = 30;
                SetExhCount = 1;
            }
            else
            {
                SetExhCount++;
            }
        }
        else
        {
            SetFreCount = 0;
            SetExpCount = 0;
            SetExhCount = 0;
        }

        if (SetFreCount >= 6)
        {
            SetFreCount = 0;

            TempA = GetValueJudge1();

            set_comp_freq(TempA);
        }

        if (SetExpCount >= 6)
        {
            SetExpCount = 0;

            TempA = GetValueJudge1();

            set_exp_step(TempA);
        }

        if (SetExhCount >= 6)
        {
            SetExhCount = 0;

            TempA = GetValueJudge1();

            set_exh_temp(TempA);
        }
    }
    else
    {
        SetFreCount = 0;
        SetExpCount = 0;
        SetExpCount = 0;
    }
}

void SleepJudge(void)
{
    U16 TempA;

    if (f_chg_sleep == ON && b_power.set == ON)
    {
        if ((e_mode.set == MODECOOL && l_tarTemp_C.set[e_mode.set] == 160) || (e_mode.set == MODEHEAT && l_tarTemp_C.set[e_mode.set] == 300))
        {
            if (SetInFanTime == 0)
            {
                SetInFanTime = 30;
                SetInFanCount = 1;
            }
            else
            {
                SetInFanCount++;
            }
        }
        else if ((e_mode.set == MODECOOL && l_tarTemp_C.set[e_mode.set] == 170) || (e_mode.set == MODEHEAT && l_tarTemp_C.set[e_mode.set] == 290))
        {
            if (SetOutFanTime == 0)
            {
                SetOutFanTime = 30;
                SetOutFanCount = 1;
            }
            else
            {
                SetOutFanCount++;
            }
        }
        else if ((e_mode.set == MODECOOL && l_tarTemp_C.set[e_mode.set] == 180) || (e_mode.set == MODEHEAT && l_tarTemp_C.set[e_mode.set] == 280))
        {
            if (SetSwStepTime == 0)
            {
                SetSwStepTime = 30;
                SetSwStepCount = 1;
            }
            else
            {
                SetSwStepCount++;
            }
        }
        else
        {
            SetInFanCount = 0;
            SetOutFanCount = 0;
            SetSwStepCount = 0;
        }

        if (SetInFanCount >= 6)
        {
            SetInFanCount = 0;

            TempA = GetValueJudge2();

            set_in_fan(TempA);
        }

        if (SetOutFanCount >= 6)
        {
            SetOutFanCount = 0;

            TempA = GetValueJudge2();

            set_out_fan(TempA);
        }

        if (SetSwStepCount >= 6)
        {
            SetSwStepCount = 0;

            TempA = GetValueJudge2() / 10;

            set_sw_step(TempA);
        }
    }
    else
    {
        SetInFanCount = 0;
        SetOutFanCount = 0;
        SetSwStepCount = 0;
    }
}

U16 GetValueJudge1(void)
{
    U16 mtemp16;

    mtemp16 = ((SetTestTimeMin & 0xF0) >> 4);
    mtemp16 = mtemp16 * 10;
    mtemp16 = mtemp16 + (SetTestTimeMin & 0x0F);
    mtemp16 = mtemp16 * 10;
    mtemp16 = mtemp16 + (SetTestTimeHour & 0x0F);

    return mtemp16;
}

U16 GetValueJudge2(void)
{
    U16 mtemp16;

    mtemp16 = ((SetTestTimeHour & 0xF0) >> 4);
    mtemp16 = mtemp16 * 10 + (SetTestTimeHour & 0x0F);
    mtemp16 = mtemp16 * 100;
    mtemp16 = mtemp16 + ((SetTestTimeMin & 0x0F) * 10);

    return mtemp16;
}

void CaptestSet(void)
{
    U8 TempA = 0;

    if (f_SetCapTest != 0)
    {
        if (e_mode.set == MODECOOL)
        {
            TempA = l_tarTemp_C.set[e_mode.set] / 10 - 15;

            if (TempA > 10)
            {
                TempA = 0;
            }
        }
        else if (e_mode.set == MODEHEAT)
        {
            TempA = 31 - l_tarTemp_C.set[e_mode.set] / 10;

            if (TempA > 10)
            {
                TempA = 0;
            }
        }

        set_captest(TempA);
    }
}

void TestRunSet(void)
{
    if (TestRunStat == 0 && parameter_out.indoorNum > 1 && CapTestStat == 0 && SetFreValue == 0)
    {
        if (e_mode.set == MODECOOL && b_power.set == ON && f_OutErr_Ub == 0 && f_chg_tartemp == TRUE)
        {
            if (SetTestRunTime == 0)
            {
                if (l_tarTemp_C.set[e_mode.set] == 170)
                {
                    SetTestRunTime = 50;
                    SetTestRunCount = 1;
                }
            }
            else
            {
                if (l_tarTemp_C.set[e_mode.set] == 160 || l_tarTemp_C.set[e_mode.set] == 170)
                {
                    SetTestRunCount++;
                }
                else
                {
                    SetTestRunTime = 0;
                    SetTestRunCount = 0;
                }

                if (SetTestRunCount >= 6)
                {
                    TestRunStat = 1;
                    SetTestRunTime = 0;
                    SetTestRunCount = 0;
                }
            }
        }
        else
        {
            SetTestRunTime = 0;
            SetTestRunCount = 0;
        }
    }
}

void eepRomReset(void)
{
    if (e_mode.set == MODEDRY && l_tarTemp_C.set[e_mode.set] == 300)
    {
        if (SeteepRomTime == 0)
        {
            if (f_chg_light == TRUE && f_chg_sleep == OFF)
            {
                SeteepRomTime = 50;
                SeteepRomCount = 1;
            }
        }
        else
        {
            if (f_chg_light == TRUE && f_chg_sleep == OFF && SeteepRomCount == 2)
            {
                SeteepRomCount++;
            }
            else if (f_chg_light == FALSE && f_chg_sleep == ON && (SeteepRomCount == 1 || SeteepRomCount == 3))
            {
                SeteepRomCount++;
            }
            else
            {
                SeteepRomTime = 0;
                SeteepRomCount = 0;
            }
        }

        if (SeteepRomCount >= 4)
        {
            SeteepRomCount = 0;

            BuzzerAct(BUZZER400ms);

            if (f_eepRom == 1)
            {
                f_eepRom = 0;

                f_code_o0 = 1;
                f_code_o1 = 0;
            }
            else
            {
                f_eepRom = 1;

                f_code_o0 = 0;
                f_code_o1 = 1;
            }

            f_eepRomRst = 1;
            f_eepRomW = 1;
        }
    }
}

void JumpTypeReset(U8 value)
{
    if (e_mode.set == MODEFAN && l_tarTemp_C.set[e_mode.set] == 310 && value > 0 && value <= JUMPTYPE)
    {
        if (f_chg_light == TRUE)
        {
            if (SetJumpTypeTime == 0)
            {
                SetJumpTypeTime = 30;
                SetJumpTypeCount = 1;
            }
            else
            {
                SetJumpTypeCount++;
            }
        }
        else
        {
            SetJumpTypeTime = 0;
            SetJumpTypeCount = 0;
        }

        if (SetJumpTypeCount >= 6)
        {
            SetJumpTypeCount = 0;

            BuzzerAct(BUZZER400ms);

            jumpType = value;
            f_jumpTypeRst = 1;
            f_eepRomW = 1;

            dispTimer.jump = 30;
            f_TxdResetReq = 1;

            abiltySet();
            fanSpeedSet();
            swingAglSet();
        }
    }
    else
    {
        SetJumpTypeTime = 0;
        SetJumpTypeCount = 0;
    }
}

void TempRepairReset(void)
{
    if (e_mode.set == MODEHEAT && l_tarTemp_C.set[e_mode.set] <= 210 && l_tarTemp_C.set[e_mode.set] >= 170)
    {
        if (SetTempRepairTime == 0)
        {
            if (f_chg_light == TRUE && f_chg_sleep == OFF)
            {
                SetTempRepairTime = 30;
                SetTempRepairCount = 1;
            }
        }
        else
        {
            if (f_chg_light == TRUE && f_chg_sleep == OFF && SetTempRepairCount == 2)
            {
                SetTempRepairCount++;
            }
            else if (f_chg_light == FALSE && f_chg_sleep == ON && (SetTempRepairCount == 1 || SetTempRepairCount == 3))
            {
                SetTempRepairCount++;
            }
            else
            {
                SetTempRepairTime = 0;
                SetTempRepairCount = 0;
            }
        }

        if (SetTempRepairCount >= 4)
        {
            SetTempRepairCount = 0;

            if (l_tarTemp_C.set[e_mode.set] == 170)
            {
                set_correct(4);
            }
            else if (l_tarTemp_C.set[e_mode.set] == 180)
            {
                set_correct(3);
            }
            else if (l_tarTemp_C.set[e_mode.set] == 190)
            {
                set_correct(2);
            }
            else if (l_tarTemp_C.set[e_mode.set] == 200)
            {
                set_correct(1);
            }
            else if (l_tarTemp_C.set[e_mode.set] == 210)
            {
                set_correct(0);
            }
        }
    }
}

void tarTemp_limit_set(void)
{
    if ((e_mode.set == MODECOOL && l_tarTemp_C.set[e_mode.set] == 310) || (e_mode.set == MODEHEAT && l_tarTemp_C.set[e_mode.set] == 160))
    {
        if (f_chg_light == TRUE)
        {
            if (SettarTempTime == 0)
            {
                SettarTempTime = 30;
                SettarTempCount = 1;
            }
            else
            {
                SettarTempCount++;
            }
        }
        else
        {
            SettarTempTime = 0;
            SettarTempCount = 0;
        }

        if (SettarTempCount >= 6)
        {
            SettarTempCount = 0;

            BuzzerAct(BUZZER400ms);

            if (f_tarTempLimit == 1)
            {
                f_tarTempLimit = 0;

                f_code_o8 = 1;
                f_code_o7 = 0;
            }
            else
            {
                f_tarTempLimit = 1;

                f_code_o8 = 0;
                f_code_o7 = 1;
            }

            f_eepRomW = 1;
        }
    }
}

void tuya_app_set_test(void) /* called in iRemote.c */
{
    /* 除湿模式关机，目标温度17  */
    if (e_mode.set == MODEDRY && b_power.set == OFF && l_tarTemp_C.set[e_mode.set] == 170)
    {
        if (app_set_test_Time == 0)
        {
            if (f_chg_light == TRUE)
            {
                app_set_test_Time = 30;
                app_set_test_Count = 1;
            }
        }
        else
        {
            if (f_chg_light == TRUE)
            {
                app_set_test_Count++;
            }
            else
            {
                app_set_test_Time = 0;
                app_set_test_Count = 0;
            }
        }

        if (app_set_test_Count >= 6)
        {
            app_set_test_Time = 0;
            app_set_test_Count = 0;

            if (setAppSetTest)
            {
                set_app_set_test(OFF);
            }
            else
            {
                set_app_set_test(ON);
            }
        }
    }
    else
    {
        app_set_test_Time = 0;
        app_set_test_Count = 0;
    }
}

void tuya_app_show_data(void) /* called in iRemote.c */
{
    /* 除湿模式关机，目标温度16  */
    if (e_mode.set == MODEDRY && b_power.set == OFF && l_tarTemp_C.set[e_mode.set] == 160)
    {
        if (app_show_data_Time == 0)
        {
            if (f_chg_light == TRUE)
            {
                app_show_data_Time = 30;
                app_show_data_Count = 1;
            }
        }
        else
        {
            if (f_chg_light == TRUE)
            {
                app_show_data_Count++;
            }
            else
            {
                app_show_data_Time = 0;
                app_show_data_Count = 0;
            }
        }

        if (app_show_data_Count >= 6)
        {
            app_show_data_Time = 0;
            app_show_data_Count = 0;

            if (setAPPShowData)
            {
                set_app_show_data(OFF);
            }
            else
            {
                set_app_show_data(ON);
            }
        }
    }
    else
    {
        app_show_data_Time = 0;
        app_show_data_Count = 0;
    }
}

void ClrTestValue(void)
{
    set_captest(0);
    set_comp_freq(0);
    set_exp_step(0);
    set_exh_temp(0);
    set_in_fan(0);
    set_out_fan(0);
    set_sw_step(0);

    f_TestFanChg = 0;
    f_TestSwChg = 0;
}

void TestSetTime(void)
{
    if (SetFreTime > 0)
    {
        SetFreTime--;
    }

    if (SetExpTime > 0)
    {
        SetExpTime--;
    }

    if (SetExhTime > 0)
    {
        SetExhTime--;
    }

    if (SetInFanTime > 0)
    {
        SetInFanTime--;
    }

    if (SetOutFanTime > 0)
    {
        SetOutFanTime--;
    }

    if (SetSwStepTime > 0)
    {
        SetSwStepTime--;
    }

    if (SetTestRunTime > 0)
    {
        SetTestRunTime--;
    }

    if (SeteepRomTime > 0)
    {
        SeteepRomTime--;
    }

    if (SetJumpTypeTime > 0)
    {
        SetJumpTypeTime--;
    }

    if (SetTempRepairTime > 0)
    {
        SetTempRepairTime--;
    }

    if (SettarTempTime > 0)
    {
        SettarTempTime--;
    }

    if (app_set_test_Time > 0)
    {
        app_set_test_Time--;
    }

    if (app_show_data_Time > 0)
    {
        app_show_data_Time--;
    }
}
